
const express = require('express');
const router = express.Router();

const CasetemplateModel = require('../db/CasetemplateModel');

/**
 * 病例模版路由
 * CRUD
 */

//增加模版
router.post('/save', function (req, res) {
    const temp = new CasetemplateModel(req.body);
    const name = req.body.name;
    CasetemplateModel.find({ name: name }).then(casetemplate => {
        if (casetemplate == null) {
            temp.save().then(data => {
                return res.json({
                    code: 200,
                    mes: '创建模版成功.'
                });
            }).catch(err => {
                return res.json({
                    code: 201,
                    mes: '创建模版失败.'
                });
            })
        } else {
            return res.json({
                code: 202,
                mes: '此模版已存在!!'
            });
        }
    }).catch(err => {
        return res.json({
            code: 203,
            mes: '数据库繁忙,请稍后重试.'
        });
    })

});

//删除模版
router.post('/delete', function (req, res) {
    const id = req.body.id;
    CasetemplateModel.remove({ _id: id }).then(docs => {
        if (docs) {
            return res.json({
                code: 200,
                mes: '删除成功.'
            });
        }
        else {
            return res.json({
                code: 201,
                mes: '删除失败.'
            });
        }
    }).catch(err => {
        return res.json({
            code: 202,
            mes: "数据库繁忙,请稍后重试.",
            error: err
        });
    })
});


//修改模版
router.post('/update', function (req, res) {
    const id = req.body.id;
    const temp = req.body;
    CasetemplateModel.findOne({ _id: id }).then(casetemplate => {
        if (casetemplate != null) {
            CasetemplateModel.update({ _id: id }, { $set: temp }).then(data => {
                return res.json({
                    code: 200,
                    mes: '修改成功.'
                });
            }).catch(err => {
                return res.json({
                    code: 201,
                    mes: '修改失败.'
                });
            })
        } else {
            return res.json({
                code: 202,
                mes: '此ID不存在.'
            });
        }
    }).catch(err => {
        return res.json({
            code: 203,
            mes: '数据库繁忙,请稍后重试.'
        });
    })
});

//查询所有
router.get('/list', function (req, res) {
    CasetemplateModel.find().then(casetemplate => {
        return res.json({
            code: 200,
            mes: '查询成功.',
            data: casetemplate
        });
    }).catch(err => {
        return res.json({
            code: 201,
            mes: '数据库繁忙,查询失败.'
        });
    })
});

//根据ID查询
router.get('/One/:id', function (req, res) {
    const id = req.param.id;
    CasetemplateModel.findById(id).then(casetemplate => {
        return res.json({
            code: 200,
            mes: '查询成功.',
            data: casetemplate
        });
    }).catch(err => {
        return res.json({
            code: 201,
            mes: '查询失败.'
        });
    })
});

module.exports = router;